﻿using JNPF.Basics.Models;
using JNPF.Data;
using JNPF.Data.Util;
using JNPF.Utils;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace JNPF.Basics.Logic
{
    /// <summary>
    /// 企业号用户
    /// 版 本：V3.0.0
    /// 版 权：引迈信息技术有限公司（https://www.jnpfsoft.com）
    /// 作 者：JNPF开发平台组
    /// 日 期：2018-08-13 
    /// </summary>
    internal class QYUserService : DataFactory, QYUserIService
    {
        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        public async Task<List<QYUserEntity>> GetList()
        {
            using (var db = this.Basic)
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append(@"select
                                u.F_Id,
                                u.F_Account,
                                u.F_RealName,
                                u.F_Gender,
                                u.F_EnabledMark,
                                u.F_OrganizeId,
                                u.F_PositionId,
                                u.F_MobilePhone,
                                u.F_Description,
                                w.F_SyncState,
                                w.F_LastModifyTime
                                from  Base_User u left join 
                                WeChat_QYUser w on w.F_UserId= u.F_Id");
                return await db.GetListSqlAsync<QYUserEntity>(strSql.ToString());
            }
        }

        public async Task<PageResult<QYUserEntity>> GetList(PageInput input)
        {
            using (var db = this.Basic)
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append(@"select
                                u.F_Id,
                                u.F_Account,
                                u.F_RealName,
                                u.F_Gender,
                                u.F_EnabledMark,
                                u.F_OrganizeId,
                                u.F_PositionId,
                                u.F_MobilePhone,
                                u.F_Description,
                                w.F_SyncState,
                                w.F_LastModifyTime
                                from  Base_User u left join 
                                WeChat_QYUser w on w.F_UserId= u.F_Id");
                return await PageHelper.GetPageResultAsync<QYUserEntity>(db, input, strSql.ToString());
            }
        }

        public async Task<List<QYUserEntity>> GetSyncList()
        {
            using (var db = this.Basic)
            {
                return await db.GetListAsync<QYUserEntity>();
            }
        }
        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Create(QYUserEntity entity)
        {
            using (var db = this.Basic)
            {
                return await db.InsertAsync(entity);
            }
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Update(QYUserEntity entity)
        {
            using (var db = this.Basic)
            {
                return await db.UpdateAsync(entity);
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Delete(QYUserEntity entity)
        {
            using (var db = this.Basic)
            {
                return await db.DeleteAsync(entity);
            }
        }
    }
}
